#--clear_datastore or -c
from rer import SampleData, Categories
from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app

def remove_all_entitiy(model):
    import time
    try:
        s = 'SELECT __key__ FROM %s' % model.entity_type()
        q = db.GqlQuery(s)
        while q.count():
            r = q.fetch(limit=100)
            db.delete(r)
            q = db.GqlQuery(s)
            time.sleep(1)
    except:
        return


class ClearData(webapp.RequestHandler):
    def get(self):
        remove_all_entitiy(Categories)
        remove_all_entitiy(SampleData)
        self.redirect('/reference')


application = webapp.WSGIApplication([('/clear', ClearData)], debug=False)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()